package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")     //也可以在web.xml中封装配置信息
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
//        super();
        // TODO Auto-generated constructor stub
    	System.out.println("RegisterServlet  create........");
    }
    @Override
    public void destroy() {
    	System.out.println("RegisterServlet destroy .......");
    }
    
    @Override
    public void init(ServletConfig config) throws ServletException {
    	System.out.println("RegisterServlet init .......");
    	System.out.println(config.getInitParameter("app"));
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub		
		
		ServletContext sc=request.getServletContext();
		//String c=(String) sc.getAttribute("count");
		if(sc.getAttribute("count")!=null) {
			int count=(int)sc.getAttribute("count");
			sc.setAttribute("count", count+1);
		}else {
			sc.setAttribute("count", 1);
		}
				
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("UTF-8");    //设置输入内容的编码方式
		
		response.setContentType("text/html;charset=UTF-8");   //设置为html编码格式
		
//		String username = URLEncoder.encode(request.getParameter("username"),"utf-8"); //设置中文显示内容格式编码为utf-8
		String username=request.getParameter("username");
		
		System.out.println(username);
		
		String age=request.getParameter("age");
		
		System.out.println(age);
		
//		String sex = URLEncoder.encode(request.getParameter("sex"),"utf-8");
		String sex=request.getParameter("sex");
		
		System.out.println(sex);
		
		String email=request.getParameter("email");
		
		System.out.println(email);
		
		request.setAttribute("username", username);
		request.setAttribute("age", age);
		request.setAttribute("sex", sex);
		request.setAttribute("email", email);
		
		
		
		//注册得用户信息写入数据库
//		request.getRequestDispatcher("register_result.jsp").forward(request, response); //转发 服务器端的跳转
		
		response.sendRedirect("register_result.jsp?username="+username+"&age="+age+"&sex="+sex+"&email="+email);   //重定向  客户端的跳转
	}

}
